<?

//require_once(dirname(__FILE__) . '/../abstract/http.php');

class bf_http_curl extends bf_http {

	public function get($url, $params = array(), $auth = array()) {

		$curl_opts = array(
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_FRESH_CONNECT => true,
			CURLOPT_CONNECTTIMEOUT => $this->opts['timeout'],
			CURLOPT_HTTPHEADER => array(
				'Expect:',
			),
			//CURLOPT_VERBOSE => true,
		);

		if (isset($auth['user']) && isset($auth['password'])) {
			$curl_opts[CURLOPT_HTTPAUTH] = CURLAUTH_ANY;
			$curl_opts[CURLOPT_USERPWD] = "{$auth['user']}:{$auth['password']}";
		}

		return $this->make_request($url . '?' . (is_array($params) ? $this->build_params($params) : $params), $curl_opts);
	}

	public function post($url, $params = array(), $auth = array()) {

		$curl_opts = array(
			CURLOPT_POST => true,
			CURLOPT_POSTFIELDS => (is_array($params) ? $this->build_params($params) : $params),
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_FRESH_CONNECT => true,
			CURLOPT_CONNECTTIMEOUT => $this->opts['timeout'],
			CURLOPT_HTTPHEADER => array(
				'Expect:',
			),
			//CURLOPT_VERBOSE => true,
		);

		if (isset($auth['user']) && isset($auth['password'])) {
			$curl_opts[CURLOPT_HTTPAUTH] = CURLAUTH_ANY;
			$curl_opts[CURLOPT_USERPWD] = "{$auth['user']}:{$auth['password']}";
		}

		return $this->make_request($url, $curl_opts);
	}

	protected function make_request($url, $curl_opts) {
		$ch = curl_init($url);
		curl_setopt_array($ch, $curl_opts);
		$response = curl_exec($ch);
		$info = curl_getinfo($ch);
		$status = @$info['http_code'];
		curl_close($ch);
		return array('status' => $status, 'response' => $response);
	}
}

?>